//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "uSKiloutou.h"
#include "dm_Kiloutou.h"
#include "WebKiloutou.h"
#include "About.h"

#pragma package(smart_init)
#pragma resource "*.dfm"
TuEcran *uEcran;
Graphics::TBitmap *BrushBmpClient;
Graphics::TBitmap *BrushBmpMateriel;
Graphics::TBitmap *BrushBmpLocation;
//---------------------------------------------------------------------------
//                  CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TuEcran::TuEcran(TComponent* Owner)
    : TForm(Owner)
{
    try
    {
      //Materiel
      BrushBmpMateriel = new Graphics::TBitmap;
      BrushBmpMateriel->LoadFromFile("Images\\Materiel.bmp");
      GroupBox1->Brush->Bitmap = BrushBmpMateriel;

      //Client
      BrushBmpClient = new Graphics::TBitmap;
      BrushBmpClient->LoadFromFile("Images\\Client.bmp");
      GroupBox2->Brush->Bitmap = BrushBmpClient;

      eNomClient->Clear();
      ePrenom->Clear();
      eAge->Clear();
      gbFicheCaracteristique->Visible = false;
    } catch( Exception &e) {
        ShowMessage(e.Message);
    }

}

//---------------------------------------------------------------------------
//                  DESTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TuEcran::~TuEcran()
{
    GroupBox2->Brush->Bitmap = NULL;
    delete BrushBmpClient;

    GroupBox1->Brush->Bitmap = NULL;
    delete BrushBmpMateriel;
}
//---------------------------------------------------------------------------
//              AJOUT D UN CLIENT
//---------------------------------------------------------------------------
void __fastcall TuEcran::bAjouterClientClick(TObject *Sender)
{
    CKiloutouClient * Clients = NULL;
    try
    {
      if(    !eNomClient->Text.IsEmpty()
          && !ePrenom->Text.IsEmpty()
          && !eAge->Text.IsEmpty()) {
        Clients = new CKiloutouClient();
        Clients->Insert( eNomClient->Text, ePrenom->Text, eAge->Text.ToInt());
      }
      else {
        MessageDlg("Veuillez remplir les champs Nom, Prénom et Age", mtWarning, TMsgDlgButtons() << mbOK, 0);
      }
    } catch( Exception &e) {
        ShowMessage((AnsiString)"Erreur  : "+e.Message);
    }
    if(Clients) {delete Clients; Clients = NULL;}
}
//---------------------------------------------------------------------------
//              SUPPRESSION D UN CLIENT
//---------------------------------------------------------------------------

void __fastcall TuEcran::bSupprimerClientClick(TObject *Sender)
{
    CKiloutouClient * Clients = NULL;
    try
    {
      if( (MessageDlg("Etes-vous sûr de supprimer ce client ?", mtWarning, TMsgDlgButtons() << mbYes << mbNo, 0)) == mrYes)
      {
        Clients = new CKiloutouClient();
        Clients->Delete( lbClients );
      }
      eNomClient->Clear();
      ePrenom->Clear();
      eAge->Clear();
    } catch( Exception &e) {
        ShowMessage((AnsiString)"Plus de clients  : "+e.Message);
    }
    if(Clients) {delete Clients; Clients = NULL;}
}
//---------------------------------------------------------------------------
//              MODIFICATION D UN CLIENT
//---------------------------------------------------------------------------


void __fastcall TuEcran::bModifierClientClick(TObject *Sender)
{
    CKiloutouClient * Clients = NULL;
    if(    !eNomClient->Text.IsEmpty()
        && !ePrenom->Text.IsEmpty()
        && !eAge->Text.IsEmpty()) {
      if( (MessageDlg("Etes-vous sûr de modifier ce client ?", mtWarning, TMsgDlgButtons() << mbYes << mbNo, 0)) == mrYes)
      {
          Clients = new CKiloutouClient();
          Clients->Update ( eNomClient->Text, ePrenom->Text, eAge->Text.ToInt());
          delete Clients;
      }
    }
    else {
        MessageDlg("Veuillez sélectionnez un client", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }
    eNomClient->Clear();
    ePrenom->Clear();
    eAge->Clear();
}
//---------------------------------------------------------------------------
//              AJOUTER UN MATERIEL
//---------------------------------------------------------------------------
void __fastcall TuEcran::bAjouterClick(TObject *Sender)
{
    switch( GererSaisie() )
        {
            case SAISIE_Camion:
                {
                      CCamion * Camion = NULL;
                      Camion = new CCamion();
                      Camion->Ajouter();
                      if (Camion) {
                         delete Camion;
                         Camion = NULL;
                      }
                }
                break;

            case SAISIE_Tondeuse:
                {
                      CTondeuse * Tondeuse = NULL;
                      Tondeuse = new CTondeuse();
                      Tondeuse->Ajouter();
                      if (Tondeuse) {
                        delete Tondeuse;
                        Tondeuse = NULL;
                      }
                }
                break;

            case SAISIE_Pelleteuse:
                {
                      CPelleteuse * Pelleteuse = NULL;
                      Pelleteuse = new CPelleteuse();
                      Pelleteuse->Ajouter();
                      if (Pelleteuse) {
                        delete Pelleteuse;
                        Pelleteuse = NULL;
                      }
                }
                break;
        }
}
//---------------------------------------------------------------------------
//              MODIFIER UN TYPE DE MACHINE
//---------------------------------------------------------------------------

void __fastcall TuEcran::bModifierClick(TObject *Sender)
{
    switch(dmKiloutou->tMaterielTYPE_MATERIEL->AsInteger)
    {
        case TYPE_Camion:
            {
                CCamion * Camion = NULL;
                Camion = new CCamion();
                Camion->Modifier();
                if(Camion) { delete Camion; Camion = NULL; }
            }
            break;

        case TYPE_Tondeuse:
            {
                CTondeuse * Tondeuse = NULL;
                Tondeuse = new CTondeuse();
                Tondeuse->Modifier();
                if(Tondeuse) { delete Tondeuse; Tondeuse = NULL; }
            }
            break;

        case TYPE_Pelleteuse:
            {
                CPelleteuse * Pelleteuse = NULL;
                Pelleteuse = new CPelleteuse();
                Pelleteuse->Modifier();
                if(Pelleteuse) { delete Pelleteuse; Pelleteuse = NULL; }
            }
            break;
    }
}
//---------------------------------------------------------------------------
//              SUPPRIMER UN TYPE DE MACHINE
//---------------------------------------------------------------------------
void __fastcall TuEcran::bSupprimerMaterielClick(TObject *Sender)
{
    switch(dmKiloutou->tMaterielTYPE_MATERIEL->AsInteger)
    {
        case TYPE_Camion:
            {
                CCamion * Camion = NULL;
                Camion = new CCamion();
                Camion->Supprimer();
                if(Camion) { delete Camion; Camion = NULL; }
            }
            break;

        case TYPE_Tondeuse:
            {
                CTondeuse * Tondeuse = NULL;
                Tondeuse = new CTondeuse();
                Tondeuse->Supprimer();
                if(Tondeuse) { delete Tondeuse; Tondeuse = NULL; }
            }
            break;

        case TYPE_Pelleteuse:
            {
                CPelleteuse * Pelleteuse = NULL;
                Pelleteuse = new CPelleteuse();
                Pelleteuse->Supprimer();
                if(Pelleteuse) { delete Pelleteuse; Pelleteuse = NULL; }
            }
            break;
    }
}

//---------------------------------------------------------------------------
//              FICHE CARACTERISTIQUE DU MATERIEL
//---------------------------------------------------------------------------
void __fastcall TuEcran::Caractristiques1Click(TObject *Sender)
{
    gbFicheCaracteristique->Visible = true;
    switch(dmKiloutou->tMaterielTYPE_MATERIEL->AsInteger)
    {
        case TYPE_Camion:
            {
                eNomMachine->Text           = dmKiloutou->tMaterielNOM->AsString;
                mDescription->Text                 = dmKiloutou->tMaterielDESCRIPTION->AsString;
                ePrix->Text                 = dmKiloutou->tMaterielPRIX->AsFloat;
                ePoidsTotal->Text           = dmKiloutou->tCamionPOIDSTOTAL->AsFloat;
                ePoidsTransportable->Text   = dmKiloutou->tCamionPOIDSTRANSPORTABLE->AsFloat;
                eVitesse->Visible = false;
                lVitesse->Visible = false;
            }
            break;

        case TYPE_Tondeuse:
            {
                eNomMachine->Text           = dmKiloutou->tMaterielNOM->AsString;
                mDescription->Text                 = dmKiloutou->tMaterielDESCRIPTION->AsString;
                ePrix->Text                 = dmKiloutou->tMaterielPRIX->AsFloat;
                eVitesse->Text              = dmKiloutou->tTondeuseVITESSE->AsFloat;
                ePoidsTotal->Visible        = false;
                ePoidsTransportable->Visible = false;
                lPoidsTotal->Visible = false;
                lPoidsTransportable->Visible = false;
            }
            break;

        case TYPE_Pelleteuse:
            {
                eNomMachine->Text           = dmKiloutou->tMaterielNOM->AsString;
                mDescription->Text                 = dmKiloutou->tMaterielDESCRIPTION->AsString;
                ePrix->Text                 = dmKiloutou->tMaterielPRIX->AsFloat;
                ePoidsTotal->Text           = dmKiloutou->tPelleteusePOIDSTOTAL->AsFloat;
                eVitesse->Visible = false;
                lVitesse->Visible = false;
                ePoidsTransportable->Visible = false;
                lPoidsTransportable->Visible = false;

            }
            break;
    }
}

//---------------------------------------------------------------------------
//              FERMER LES CARACTERISTIQUE DES MATERIELS
//---------------------------------------------------------------------------
void __fastcall TuEcran::bFermerCaracClick(TObject *Sender)
{
    gbFicheCaracteristique->Visible = false;
}

//---------------------------------------------------------------------------
//              AJOUTER UNE LOCATION
//---------------------------------------------------------------------------


void __fastcall TuEcran::bAjouterLocationClick(TObject *Sender)
{
    CLocation * Location = NULL;
    Location = new CLocation();
    Location->Ajouter();
    if(Location)
    {
        delete Location;
        Location = NULL;
    }
}
//---------------------------------------------------------------------------
//              RENDRE UNE LOCATION
//---------------------------------------------------------------------------
void __fastcall TuEcran::bRetourLocationClick(TObject *Sender)
{
    CLocation * Location = NULL;
    Location = new CLocation();
    Location->Rendre();
    if(Location)
    {
        delete Location;
        Location = NULL;
    }
}
//---------------------------------------------------------------------------
//              STATISTIQUE DES LOCATIONS
//---------------------------------------------------------------------------
void __fastcall TuEcran::bStatistiqueClick(TObject *Sender)
{
    TvAStatistiques * Statistiques = NULL;
    Statistiques = new TvAStatistiques( Application );
    Statistiques->ShowModal();
    if(Statistiques) { delete Statistiques; Statistiques = NULL; }
}
//---------------------------------------------------------------------------


void __fastcall TuEcran::lbClientsDblClick(TObject *Sender)
{
    eNomClient->Text    = dmKiloutou->tClientNOM->AsString;
    ePrenom->Text       = dmKiloutou->tClientPRENOM->AsString;
    eAge->Text          = dmKiloutou->tClientAGE->AsInteger;
}

void __fastcall TuEcran::TableLocation1Click(TObject *Sender)
{
    CLocation * Location = NULL;
    Location = new CLocation();
    Location->EffacerTable();
    if(Location) {delete Location; Location = NULL;}
}
//---------------------------------------------------------------------------

void __fastcall TuEcran::TableClient1Click(TObject *Sender)
{
    CKiloutouClient * Client = NULL;
    Client = new CKiloutouClient();
    Client->EffacerTable();
    lbClients->Refresh();
    if(Client) {delete Client; Client = NULL;}
}
//---------------------------------------------------------------------------

void __fastcall TuEcran::TableMatriel1Click(TObject *Sender)
{
    CCamion * Machine = NULL;
    Machine = new CCamion();
    Machine->EffacerTable();
    if(Machine) {delete Machine; Machine = NULL;}    
}
//---------------------------------------------------------------------------
//              QUITTER LE PROGRAMME
//---------------------------------------------------------------------------
void __fastcall TuEcran::Quitter1Click(TObject *Sender)
{
    uEcran->Close();
}
//---------------------------------------------------------------------------


void __fastcall TuEcran::Apropos1Click(TObject *Sender)
{
    TvAAbout * fAbout = NULL;
    fAbout = new TvAAbout(Application);
    fAbout->ShowModal();
    if(fAbout) { delete fAbout; fAbout = NULL;}
}
//---------------------------------------------------------------------------

void __fastcall TuEcran::Siteinternet1Click(TObject *Sender)
{
    TvASiteKiloutou * SiteKiloutou = NULL;
    SiteKiloutou = new TvASiteKiloutou(Application);
    SiteKiloutou->ShowModal();
    if(SiteKiloutou) {delete SiteKiloutou; SiteKiloutou = NULL;}
}
//---------------------------------------------------------------------------

